package com.haoke.buyaction.message.consumer.listener;

import com.haoke.buyaction.message.consumer.service.BuyactionService;
import com.haoke.commons.message.HaokeBuyMessage;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 购买消息消费者
 *
 * @author yue13
 */

@Component
public class HaokeBuyactionConsumer {
    @Autowired
    private BuyactionService buyactionService;

    /**
     * RabbitListener监听队列
     *
     * 都在commons目录下，resources
     *
     * @param message
     * @return
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "${haoke.rabbit.buy.queue}", autoDelete = "false"),
            exchange = @Exchange(name = "${haoke.rabbit.buy.exchange}"),
            key = "${haoke.rabbit.buy.routingKey}"
    )})

    public Object onMessage(HaokeBuyMessage message) {
        // 商品主键
        String itemId = message.getItemId();
        // 用户手机号
        String user = message.getUsername();

        boolean result = buyactionService.buyaction(itemId, user);

        return result;
    }
}
